package com.css.apps.base.common.provider;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.org.model.SOrgPost;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.model.SUserPost;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.ids.server.service.dao.IdsOrgDao;
import com.css.util.StringHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
/**
 * 单位、部门、处室查询集合
 * @author paladin
 */
public class OrgQueryProvider {
	
	private static Log log = LogFactory.getLog(OrgQueryProvider.class);
	
	public static SOrg getOrg(String orgId) {
		return QueryCache.get(SOrg.class, orgId);
	}
	
	public static String getOrgNames(String orgIds) {
		List<String> names = new ArrayList<>();
		List<String> orgIdList = StringHelper.strToList(orgIds);
		if (orgIdList != null) {
			for (String orgId : orgIdList) {
				SOrg org = getOrg(orgId);
				if (org == null) {
					continue;
				}
				names.add(org.getName());
			}
		}
		return StringHelper.listToStr(names);
	}
	
	@SuppressWarnings("unchecked")
	public static List<String> getOrganIds(String parentId) {
		QueryCache qc = new QueryCache("select a.uuid from SOrg a where a.parentId=:parentId and a.delFlag='2'")
				.setParameter("parentId", parentId);
		return qc.list();
	}
	
	/**
	 * 获取总部单位、部门、处室
	 * @author liuhy 2019-2019年3月15日
	 */
	public static List<SOrg> listHeaderQuearterOrg() {
		return IdsOrgDao.listAllOrg(1);
	}
	
	/**
	 * 获取所有单位、部门、处室
	 * @author liuhy 2019-2019年3月15日
	 */
	public static List<SOrg> listAllOrg() {
		return IdsOrgDao.listAllOrg();
	}
	
	/**
	 * 获取机构岗位列表
	 * @param orgId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<SPost> queryPost(String orgId){
		if(StringHelper.isEmpty(orgId)){
			return null;
		}
		List<SPost> postList = QueryCache.get(SOrg.class,orgId).getPostList().getList();	
		return postList;
	}

	/**
	 * 获取机构岗位ID列表
	 * @param orgId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> queryPostId(String orgId){
		if(StringHelper.isEmpty(orgId)){
			return null;
		}
		List<String> postIdList = QueryCache.get(SOrg.class,orgId).getPostList().getListById();	
		return postIdList;
	}

	/**
	 * 查询机构下业务用户列表
	 * @param orgId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public  static List<SUser> queryOrgBizUserList(String orgId){
		List<String> idlist = new QueryCache("select a.uuid from SUser a where a.orgId=:orgId and userType =  :userType and  a.delFlag = :delFlag order by a.orderNum desc")
			.setParameter("orgId", orgId).setParameter("userType", BaseEnvironment.USERTYPE_NORMAL).setParameter("delFlag", Environment.UN_DELETE_STATE).listCache();
		List<SUser> uplist = QueryCache.idToObj(SUser.class, idlist);	
		return uplist;
	}

	/**
	 * 常用联系人/岗位
	 * @return boolean
	 */
	@SuppressWarnings("unchecked")
	public static List<Object[]> checkRelate(String relateType){
		SUser sUser = UserQueryProvider.currentUser();
		QueryCache qc = new QueryCache("select relateId,relateOrgId from UserRelate where userId = :userId and relateType = :relateType")
			.setParameter("userId", sUser.getUserId()).setParameter("relateType", relateType);
		if(qc.list() != null && qc.list().size() > 0){
			return qc.list();
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static List<SOrg> queryOrgsByUserId(String userId) {
		SUser user = QueryCache.get(SUser.class, userId);
    	if(user != null) {
    		return user.getOrgList().getList();
    	}
    	return null;
	}
	
	@SuppressWarnings("unchecked")
	public static List<SUserPost> getUserByOrgPostIdList(String orgId, String postId) {
		List<String> listt = new QueryCache("select a.uuid from SUserPost  a where a.orgId=:orgId and a.postId =:postId")
				.setParameter("postId", postId).setParameter("orgId", orgId).listCache();
		return QueryCache.idToObj(SUserPost.class, listt);
	}
	
	@SuppressWarnings("unchecked")
	public static List<SOrgPost> getPostByPostIdList(String postId) {
		List<SOrgPost> orgpostList = null;
		try {
			QueryCache qc = new QueryCache("select a.uuid from SOrgPost  a where a.postId=:postId ")
					.setParameter("postId", postId);
			List<Integer> Ids = qc.listCache();
			if (Ids != null)
				orgpostList = QueryCache.idToObj(SOrgPost.class, Ids);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return orgpostList;
	}

	//-----------------------------------------------------------------------------------------------------------------------------
	public static List<SOrg> listDeptsByUser(String userId){
		List<SOrg>  orgList = new ArrayList<SOrg>();
		//查询当前用户的depts
		List<String> deptIdList = new QueryCache("select a.deptId from SUserOrg a where a.userId =:userId").setParameter("userId",userId).list();
		for (String deptId : deptIdList) {
			SOrg sorg = QueryCache.get(SOrg.class,deptId);
			if(sorg!=null){
				orgList.add(sorg);
			}
		}
		return orgList;
	}

	public static String getOrgNameByOrgId(String orgId){
		SOrg sOrg = QueryCache.get(SOrg.class,orgId);
		return  sOrg !=null ? sOrg.getName() : "";
	}

}